Page({

  /**
   * 页面的初始数据
   */
  data: {
    sorttype:0,
    hasMore:true,
    page:1,
    list:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.login({
      success: function (res) {
        if (res.code) {
          //发起网络请求
          wx.request({
            url: 'https://m.nongjike.cn/NJK/app/access_tokenss',
            data: {
              code: res.code
            },
            success:function(res){
              var data = res.data;
              if(data.code!=1||!data.unionid||!data.openid){
                wx.hideLoading();
                wx.showModal({
                  title: '提示',
                  content: '获取UNIONID失败，请重启小程序后重试！',
                });
                return false;
              }
              if (!data.unionid || !data.openid) {
                wx.hideLoading()
                wx.showModal({
                  title: '提示',
                  content: '获取UNIONID失败，请关注农极客公众号后重试！',
                });
                return false;
              }

              wx.setStorageSync("openid", data.openid);
              wx.setStorageSync("unionid", data.unionid);

              wx.request({
                url: 'https://m.nongjike.cn/NJK/app/findUnionid',
                data:{
                  "UNIONID":data.unionid
                },
                success:function(res){
                  console.log(res.data);
                  var data = res.data;
                  if(data.code==1){
                    if(!data.data||data.data==null||data.data==undefined){
                      wx.redirectTo({
                        url: '/pages/reg/reg',
                      })
                    }else{
                      wx.setStorageSync("USER_ID", data.data.USER_ID);
                      that.loadList();
                    }
                  }
                }
              })
            }
          })
        } else {
          console.log('获取用户登录态失败！' + res.errMsg);
          wx.showModal({
            title: '提示',
            content: '获取Code失败，请重启小程序重试！',
          })
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.showLoading({
      title: '加载数据...',
      mask:true
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page: 1,
      hasMore: true
    });
    this.loadList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("bottom");
    if(this.data.loading||!this.data.hasMore){
      return false;
    }else{
      this.setData({
        loading:true
      });
      this.loadList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },


  showsort:function(){
    this.setData({
      showsort:!this.data.showsort,
    })
  },

  hiddensort:function(){
    this.setData({
      showsort: false,
    })
  },

  selsort:function(e){
    console.log(e.currentTarget.dataset.id);
    this.setData({
      sorttype: e.currentTarget.dataset.id,
      showsort:false,
      hasMore:true,
      page:1
    });
    this.loadList();

  },

  showmenu:function(){
    this.setData({
      showmenu:true
    })
  },

  hiddenmenu:function(){
    this.setData({
      showmenu: false
    })
  },

  menu:function(e){
    var id = e.currentTarget.dataset.id;
    if(id==3){
      if(!this.data.showmenu){
        this.setData({
          showmenu: true
        });
        return false;
      }else{
        wx.navigateTo({
          url: '/pages/write/write',
        })
      }
    }
    this.setData({
      showmenu: false
    });
    if(id==1){
      wx.navigateTo({
        url: '/pages/mycomment/mycomment',
      })
    }else if(id==2){
      wx.navigateTo({
        url: '/pages/myarticle/myarticle',
      })
    }

    console.log(id);
  },

  navto:function(e){
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: id == 0 ? '/pages/allarticle/allarticle':'/pages/allcomment/allcomment',
    })
  },

  navi: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/articledetail/articledetail?id='+id,
    })
  },


  loadList: function () {//加载帖子
    // wx.showLoading({
    //   title: '正在加载',
    //   mask:true
    // });
    var USER_ID = wx.getStorageSync("USER_ID");
    if(!USER_ID){
      wx.showModal({
        title: '提示',
        content: '未获取您的用户ID，请关联您的账号！',
      });
      return false;
    }
    var that = this,
    page = this.data.page,
    sta = this.data.sorttype;

    console.log(sta)

    wx.request({
      url: 'https://m.nongjike.cn/NJK/app/findCatnest',
      data:{
        USER_ID:USER_ID,
        pageNum:page,
        YSTATUS:sta
      },
      success:function(res){
        wx.stopPullDownRefresh();
        var data = res.data;
        console.log(data);
        if(data.code==1){
          that.setData({
            img1:data.IMG1,
            img2:data.IMG2
          });

          var oldlist = that.data.list;

          if(!data.data||!data.data.object||data.data.object.length<=0){//没有数据了
            that.setData({
              loading: false,
              hasMore: false,
            })
            wx.hideLoading();
            return false;
          }

          if(page==1){//第一页内容，替换之前的list
            page++;
            var newlist = data.data.object,
            sortedlist = [];
            for(var i=0;i<newlist.length;i++){
              if (newlist[i].ZSTATUS==1){
                sortedlist.unshift(newlist[i]);
              }else{
                sortedlist.push(newlist[i]);
              }
            }

            that.setData({
              list:sortedlist,
              page:page,
              loading: false,
              hasMore: true
            })
          }else{//非第一页内容，加在前面的list中
            page++;
            var newlist = data.data.object,
              sortedlist = [];
            for (var i = 0; i < newlist.length; i++) {
              if (newlist[i].ZSTATUS == 1) {
                oldlist.unshift(newlist[i]);
              } else {
                oldlist.push(newlist[i]);
              }
            }

            that.setData({
              list: oldlist,
              page: page,
              loading: false,
              hasMore: true
            })
          }
        }else{
          wx.showModal({
            title: '提示',
            content: data.message,
          })
        }
        wx.hideLoading()
      }
    })
    
  },
})